import axios from 'axios'
import { Toast } from 'vant';

let host;
if (process.env.NODE_ENV == 'development') {
  host = "http://shop.bufantec.com/bufan"
} else {
  host = "http://shop.bufantec.com/bufan"
}

const service = axios.create({
  baseURL: host,
  setTimeout: 5 * 1000
})

let toast1;
let toast2;
service.interceptors.request.use(config => {
  Toast.allowMultiple();
  if (config.url === '/cart/deleteAction') {
    toast2 = Toast.success('删除成功')
  } else {
    toast1 = Toast.loading({
      message: '加载中...',
      forbidClick: true,
      loadingType: 'spinner',
    })
  }

  if (config.method.toLowerCase() == 'get') {
    config.params = config.data
  }
  return config
}, error => {
  return Promise.reject(error)
})


service.interceptors.response.use(config => {
  toast1.clear();
  if (config.config.url === '/cart/deleteAction') {
    toast2.clear();
  }


  return config.data
}, error => {
  return Promise.reject(error)
})


export default service



